package com.zx.map.controller;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.zx.map.model.SaveAgentLeftRequestModel;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import com.zx.map.utils.CommonResponseUtils;
import com.zx.map.service.AgentInfoService;
import com.zx.map.service.AgentLeftService;
import com.zx.map.service.model.AgentInfoModel;
import com.zx.map.service.model.ProductLeftModel;
import com.zx.platform.service.model.RolesModel;
import com.zx.utils.WebUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author ysm
 */
@Controller
@RequestMapping("/manage/agentLeft")
public class AgentLeftController {
	private static Logger log = Logger.getLogger(AgentLeftController.class);

	/**
	 * 流寇
	 */
	@Autowired
	private AgentLeftService agentLeftService;

	@Autowired
	private AgentInfoService agentInfoService;

	
	@RequestMapping("/queryAgentLeft")
	@ResponseBody
	public HashMap<String, Object> queryAgentLeft(Model model, HttpServletRequest request,RolesModel role) {
		try {
			//查找当前用户对应的代理商
			AgentInfoModel curAgentInfoModel=WebUtils.queryCurrentLoginAgentInfo(request);
   
            List<ProductLeftModel> list = agentLeftService.getModelByAgentId(curAgentInfoModel,role.getRoleId());
			
			return CommonResponseUtils.successResponse(list);
		} catch (Exception e) {
			log.error("内部异常", e);
			return CommonResponseUtils.failureResponse();
		}
	}
	
	
	@RequestMapping(value = "/saveOrUpdate")
    @ResponseBody
    public HashMap<String, Object> saveOrUpdate(@RequestBody SaveAgentLeftRequestModel requestModel, HttpServletRequest request) {
        try {
	        AgentInfoModel curAgentInfoModel=WebUtils.queryCurrentLoginAgentInfo(request);

	        return agentLeftService.saveOrUpdate(curAgentInfoModel,requestModel.getRoleId(),requestModel.getAgentLeftList());

        } catch (Exception e) {
            log.error("新增信息失败" + e.getMessage(), e);
         return CommonResponseUtils.failureResponse("保存失败");
        }
    }
	
}
